package dao;

import domain.News;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import util.JDBCUtils;

import java.util.List;

public class NewsDaoImpl implements NewsDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    public List<News> getAllNews() {
        String sql = "select * from news";
        List<News> newsList = template.query(sql, new BeanPropertyRowMapper<News>(News.class));
        return newsList;
    }

    public List<News> getNewsByTopic(int ntid) {
        String sql = "SELECT * FROM news where ntid = ? ORDER BY nmodifyDate DESC LIMIT 0,6";
        List<News> newsList = template.query(sql, new BeanPropertyRowMapper<News>(News.class), ntid);
        return newsList;
    }

    public int countNews(int tid) {
        String sql = "select count(*) from news";
        if (tid == 0) {
            Integer count = template.queryForObject(sql, Integer.class);
            return count;
        } else {
            sql = "select count(*) from news where ntid = ?";
            Integer count = template.queryForObject(sql, Integer.class, tid);
            return count;
        }
    }

    public News getNewsDetailsByNid(int nid) {
        String sql = "select * from news where nid = ?";
        List<News> newsList = template.query(sql, new BeanPropertyRowMapper<News>(News.class), nid);
        return newsList.get(0);
    }

    public int modifyNews(News news) {
        String sql = "update news set ntid = ?,ntitle = ?,nauthor = ?,nsummary = ?,ncontent = ?,nmodifyDate = ? where nid = ?";
        int ntid = news.getNtid();
        String ntitle = news.getNtitle();
        String nauthor = news.getNauthor();
        String nsummary = news.getNsummary();
        String ncontent = news.getNcontent();
        String nmodifyDate = news.getNmodifyDate();
        int nid = news.getNid();
        return template.update(sql, ntid, ntitle, nauthor, nsummary, ncontent, nmodifyDate, nid);
    }

    public int addNews(News news) {
        int ntid = news.getNtid();
        String ntitle = news.getNtitle();
        String nauthor = news.getNauthor();
        String nsummary = news.getNsummary();
        String ncontent = news.getNcontent();
        String ncreateDate = news.getNcreateDate();
        String nmodifyDate = news.getNmodifyDate();
        String sql = "insert into news(ntid,ntitle,nauthor,nsummary,ncontent,ncreateDate,nmodifyDate) values(?,?,?,?,?,?,?)";
        return template.update(sql, ntid, ntitle, nauthor, nsummary, ncontent,ncreateDate,nmodifyDate);
    }

    public int deleteNewsByNid(int nid) {
        //先删除该文章的所有评论，否则会因为外键约束无法删除新闻
        String sql = "delete from comments where cnid = ?";
        int count = template.update(sql,nid);
        System.out.println("有" + count + "条评论被删除,nid=" + nid);
        //删除新闻
        sql = "delete from news where nid = ?";
        return template.update(sql, nid);
    }

    public List<News> getNewsByNumber(int tid, int no, int size) {
        String sql = "SELECT * FROM news where ntid = ? ORDER BY nmodifyDate DESC LIMIT ?,?";
        //tid为0时代表查询所有新闻
        if (0 == tid) {
            sql = "SELECT * FROM news ORDER BY nmodifyDate DESC LIMIT ?,?";
            List<News> newsList = template.query(sql, new BeanPropertyRowMapper<News>(News.class), (no - 1) * size, size);
            return newsList;
        }
        List<News> newsList = template.query(sql, new BeanPropertyRowMapper<News>(News.class), tid, (no - 1) * size, size);
        return newsList;
    }
}
